package com.itheima.shengxiao2;

import javax.swing.*;

public class Cookie implements Runnable {
    private Desk desk ;

    public Cookie(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {

        //死循环
        while (true){
            synchronized (desk.getObj()){
                if(desk.getCount()==0){
                    break;
                }
                //同步代码块中 锁对象是 桌子上的锁
                //看看有没有饭
                if(desk.isFan()){
                    //有饭 等着
                    try {
                        desk.getObj().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    System.out.println("厨子做了一份饭");
                    //没有饭 做饭  更改桌子的状态
                    desk.setFan(true);
                    // 并唤醒吃货吃
                    desk.getObj().notifyAll();
                }
            }
        }
    }
}
